<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Comment as commentModel;
use think\facade\Db;
use think\facade\View;

class Comment extends Base{
    // 列表
    public function index(){
        // 转为小写字母
        $general = new \common\General();
        $tabledir = $general->capital_to_underline(input('tabledir'));
        
    	$signid = input('signid'); $keyword = trim(input('keyword'));
    	$order = ['comment.sequence'=>'desc','comment.addtime'=>'desc'];
    	if(!is_numeric($signid)) $signid = 3;

		$rs = commentModel::index($tabledir,$signid,$this->lang,$keyword,$order);
		
		// 赋值当前评论模块
		$mod_title = '';
		$moduleWhere[] = ['tabledir','=',$tabledir];
		$moduleWhere[] = ['lang','=',$this->lang];
		$moduleRs = Db::name('module')->where($moduleWhere)->find();
		View::assign('mod_title',$moduleRs['title']);
		// 赋值评论状态
		View::assign('signid',$signid);

		View::assign('list',$rs);
		View::assign('total',$rs->total());
		View::assign('lastPage',$rs->lastPage());
		
        return view();
    }
	
    // 编辑
    public function edit($id=0){
    	$id = input('id'); $signid = input('signid');
        if(!request()->isPost()){
        	$rs = Db::name('comment')->where('id',$id)->find();
        	// 评论信息
        	View::assign('rs',$rs);
            // 模块信息
            View::assign('mod_rs',Db::name($rs['tabledir'])->where('id',$rs['tableid'])->find());
            // 用户
            $userRs = Db::name('user')->where('id',$rs['userid'])->find();
			View::assign('userRs',$userRs);

            return view();
        }else{
			try {
                $rs = commentModel::edit($id);
                return json(['code' => 200, 'msg' => lang('c_success'), 'data' => $rs]);
            } catch (Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
            }
        }		
	}

	// 回复
    public function reply($id=0){
    	$id = input('id'); $signid = input('signid');
        if(!request()->isPost()){
        	$rs = Db::name('comment')->where('id',$id)->find();
        	// 评论信息
        	View::assign('rs',$rs);
            // 模块信息
            View::assign('mod_rs',Db::name($rs['tabledir'])->where('id',$rs['tableid'])->find());
            // 用户
            $userRs = Db::name('user')->where('id',$rs['userid'])->find();
			View::assign('userRs',$userRs);
			// 回复列表
			$replyWhere[] = ['comment.parentid','=',$id]; $replyWhere[] = ['comment.signid','=',1];
            $replylist = Db::view('comment','*')->view('user',['avatar','nickname'],'user.id=comment.userid')->where($replyWhere)->select();
			View::assign('replylist',$replylist);

            return view();
        }else{
            try {
                $rs = commentModel::edit($id);
                return json(['code' => 200, 'msg' => lang('c_success'), 'data' => $rs]);
            } catch (Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
            }
        }		
	}

	// 单个删除
    public function delete_one(){
    	$id = input('id');
        // 参数不能为空
        if(empty($id)){
            return json(['code' => 1001, 'msg' => lang('c_error_parameter'), 'data' => array('id'=>$id)]);
        }
        
        try {
            // 删除子信息
            Db::name('comment')->where('parentid',$id)->delete();
            // 执行删除
            Db::name('comment')->where('id',$id)->delete();
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('id'=>$id)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }

	// 单个更新状态
    public function update_signid(){
    	$id = input('id'); $signid = input('signid');
        // 参数不能为空
        if(empty($id) or empty($signid)){
            return json(['code' => 1001, 'msg' => lang('c_error_parameter'), 'data' => array('id'=>$id)]);
        }
        // 软删除
        if($signid == 9) $signid = time();
        // 更新模块表
        
        try {
            $rs = Db::name('comment')->where('id',$id)->update(['signid'=>$signid]);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => $rs]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }

    // 批量更新状态
    public function updateall_signid(){
        $selectid = input('selectid/a'); $signid = input('signid');
        // 参数错误
        if(empty($selectid) or empty($signid)){
            return json(['code' => 1001, 'msg' => lang('c_error_parameter'), 'data' => array('selectid'=>$selectid)]);
        }
        // 软删除
        if($signid == 9) $signid = time();

        try {
            $rs = Db::name('comment')->where('id','in',$selectid)->update(['signid'=>$signid]);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => $rs]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }

    //批量删除
    public function delete_all(){
        $selectid = input('selectid/a');
        //参数错误
        if(empty($selectid)){
            return json(['code' => 1001, 'msg' => lang('c_delete_check'), 'data' => array('selectid'=>$selectid)]);
        }
        
        try {
            //删除子信息
            Db::name('comment')->where('parentid','in',$selectid)->delete();
            //执行删除
            Db::name('comment')->delete($selectid);
            return json(['code' => 200, 'msg' => lang('c_success'), 'data' => array('selectid'=>$selectid)]);
        } catch (Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => '']);
        }
    }
}